<template>
    <div class="userInfo-wrapped">
        <el-form ref="userForm" :model="userForm" label-width="80px">
            <el-form-item label="头像:">
                <el-upload class="avatar-uploader" action="api/upload" :show-file-list="false"
                    :on-success="handleAvatarSuccess" :headers="{ token: token }">
                    <img v-if="userForm.image" :src="userForm.image" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="账号:">
                <el-input v-model="userForm.username" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="用户名:">
                <el-input v-model="userForm.name"></el-input>
            </el-form-item>
            <el-form-item label="性别:">
                <template>
                    <el-radio v-model="userForm.gender" :label="1">男</el-radio>
                    <el-radio v-model="userForm.gender" :label="2">女</el-radio>
                </template>
            </el-form-item>
            <el-form-item label="职位:">
                <el-select v-model="userForm.job" placeholder="请选择">
                    <el-option v-for="item in jobs" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="部门:">
                <el-select v-model="userForm.deptId" placeholder="请选择">
                    <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="small" @click="onSubmit">修改</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import { GetAllDept, GetAllJobs, UpdatePrivateUserServer, UpdateUserAvatar } from '@/api/user';

export default {
    data() {
        return {
            userForm: this.$store.state.UserInfo.userInfo,
            options: [],
            jobs: [],
            token: localStorage.getItem('token')
        }
    },
    methods: {
        async onSubmit() {
            let res = await UpdatePrivateUserServer(this.userForm)
            if (res.code == 1) {
                this.$message({
                    showClose: true,
                    message: '修改成功',
                    type: 'success'
                })
                this.$store.commit('setUserInfo',this.userForm)
            } else {
                this.$message({
                    showClose: true,
                    message: res.msg ? res.msg : "修改失败",
                    type: 'error'
                })
            }
        },
        handleAvatarSuccess(res) {
            this.updatedAvatar(res.data)
        },
        async updatedAvatar(image){
            let res = await UpdateUserAvatar(image,this.userForm.username)
            if(res.code==1){
                this.userForm.image = image
                this.$store.commit('setUserInfo',this.userForm)
                this.$message({
                    showClose: true,
                    message: '头像更改成功',
                    type: 'success'
                });
            }      
        },
        async getDepartments() {
            let res = await GetAllDept()
            if (res.code == 1) {
                this.options = res.data
                console.log(this.options)
            } else {
                this.$message({
                    showClose: true,
                    message: '获取部门分类失败',
                    type: 'error'
                });
            }
        },
        async getJobs() {
            let res = await GetAllJobs()
            if (res.code == 1) {
                this.jobs = res.data
                console.log(this.jobs)
            } else {
                this.$message({
                    showClose: true,
                    message: '获取职业分类失败',
                    type: 'error'
                });
            }
        }
    },
     mounted() {
        this.getDepartments()
        this.getJobs()
    }
}
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #282828;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
</style>

<style lang="less" scoped>
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 108px;
    height: 108px;
    line-height: 108px;
    text-align: center;
}

.avatar {
    width: 108px;
    height: 108px;
    display: block;
}

.userInfo-wrapped {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-start;
    margin-left: 10%;

    .el-form-item {
        display: flex;
        align-items: center;
    }
}

.el-button--small {
    margin-left: 20%;
}

:deep(.el-input__inner) {
    height: 30px;
}

:deep(.el-form-item) {
    margin-bottom: 12px;
}
</style>